import json
import re
from datetime import datetime, timedelta
from typing import List, Optional
from app.db.database import SessionLocal
from app.db.crud import create_tpost
import requests
from bs4 import BeautifulSoup


class NewsItem:
    def __init__(self, url: str, title: str, id: str, pub_date: str):
        self.url = url
        self.title = title
        self.id = id
        self.pub_date = pub_date


class BaiduTopDataFetcher:
    def __init__(self):
        self.base_url = "https://www.ithome.com/list/"

    def fetch_and_process(self) -> List[NewsItem]:
        response = self._fetch_data(self.base_url)
        news_items = self._parse_data(response)
        return self._sort_news_items(news_items)

    def _fetch_data(self, url: str) -> str:
        response = requests.get(url)
        response.raise_for_status()
        return response.text

    def _parse_data(self, html: str) -> List[NewsItem]:
        soup = BeautifulSoup(html, "html.parser")
        news_items = []

        for li in soup.select("#list > div.fl > ul > li"):
            a_tag = li.find("a", class_="t")
            url = a_tag.get("href") if a_tag else None
            title = a_tag.text.strip() if a_tag else None
            date_tag = li.find("i")
            date = date_tag.text.strip() if date_tag else None

            if url and title and date:
                is_ad = self._is_ad(url, title)
                if not is_ad:
                    pub_date = date
                    news_items.append(NewsItem(url=url, title=title, id=url, pub_date=pub_date))
                    with SessionLocal() as db:
                        new_post = create_tpost(
                            db=db,
                            name=title,
                            title=title,
                            url=url,
                            content="",
                            short_content="",
                            is_judge=False,
                            is_user_define=False,
                            source_id=2
                        )
        return news_items

    def _is_ad(self, url: str, title: str) -> bool:
        ad_keywords = ["lapin", "神券", "优惠", "补贴", "京东"]
        return "lapin" in url or any(keyword in title for keyword in ad_keywords)



    def _sort_news_items(self, news_items: List[NewsItem]) -> List[NewsItem]:
        return sorted(news_items, key=lambda x: x.pub_date, reverse=True)


if __name__ == "__main__":

    # 创建类实例
    fetcher = BaiduTopDataFetcher()

    # 运行异步方法
    result = fetcher.fetch_and_process()

    # 打印结果
    print(json.dumps([item.__dict__ for item in result], ensure_ascii=False, indent=2))